package com.example.tliaswebmanagement.service.impl;

import com.example.tliaswebmanagement.mapper.StuMapper;
import com.example.tliaswebmanagement.pojo.*;
import com.example.tliaswebmanagement.service.StuService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StuServiceImpl implements StuService {
    private final StuMapper stuMapper;

    public StuServiceImpl(StuMapper stuMapper) {
        this.stuMapper = stuMapper;
    }

    /**
     * 分页条件查询学员
     */
    @Override
    public PageResult<Student> paginationStu(StuQueryParam stuQueryParam) {
        // 获取参数
        PageHelper.startPage(stuQueryParam.getPage(), stuQueryParam.getPageSize());
        List<Student> stulist = stuMapper.list(stuQueryParam);
        Page<Student> p = (Page<Student>) stulist;

        return new PageResult<Student>((int) p.getTotal(), p.getResult());
    }

    /**
     * 根据 ID 查询（数据回显）
     */
    @Override
    public Student findById(Integer id) {
         return stuMapper.findById(id);
    }

    /**
     * 修改学员
     */
    @Override
    public void updateById(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        stuMapper.updateById(student);
    }

    /**
     * 添加学员
     */
    @Override
    public void addStu(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        stuMapper.addStu(student);
    }

    /**
     * 批量删除学员
     */
    @Override
    public void deleteStu(List<Integer> ids) {
        stuMapper.deleteStu(ids);
    }

    /**
     * 增加违纪次数以及扣除分数
     */
    @Override
    public void violateUpdate(Integer id, Integer score) {
        // 获取该学员原本的违纪次数以及分数
        Student stu = stuMapper.findById(id);  // 调用查询接口
        Short violationCount = stu.getViolationCount();
        Short violationScore = stu.getViolationScore();
        // 更新违纪次数以及扣除分数
        Integer updateScore = violationScore + score;
        stuMapper.violateUpdate(id, updateScore);

    }
}
